<!--
 * @Author: wankong
 * @Desc: 
 * @Date: 2021-10-10 20:13:11
 * @LastEditTime: 2021-12-24 16:14:11
-->
<template>
  <div class="Tags">
    <el-row :gutter="20" style="margin-top: 30px">
      <el-col :span="3" v-for="tag in tagList" :key="tag.id">
        <MMCard class="card" :title="tag.tagname" opacity="0.8">
          <template slot="content">
            <el-button
              size="small"
              type="primary"
              icon="el-icon-edit"
              @click="showEdit(tag)"
            ></el-button>
            <el-button
              size="small"
              type="danger"
              icon="el-icon-delete"
              @click="delTag(tag)"
            ></el-button> </template
        ></MMCard>
      </el-col>
      <el-col :span="2">
        <MMCard
          class="card"
          title="添加一条"
          @click.native="addshow"
          opacity="0.8"
        >
          <template slot="content">
            <el-button
              type="success"
              icon="el-icon-plus"
            ></el-button> </template
        ></MMCard>
      </el-col>
    </el-row>
    <el-dialog
      title="填写信息"
      :visible.sync="dialogVisible"
      width="30%"
      :before-close="handleClose"
    >
      <el-form ref="tagrefs" :rules="editRule" :model="tag" label-width="100px">
        <el-form-item label="标签名称：" prop="tagname">
          <el-input v-model="tag.tagname" placeholder="在这里输入"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="changeEdit()">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import MMCard from "@/components/common/Card.vue";
export default {
  components: {
    MMCard,
  },
  data() {
    return {
      dialogVisible: false,
      tagList: [],
      tag: {},
      editRule: {
        tagname: {
          required: true,
          message: "标签名要填啊。。",
          trigger: "blur",
        },
      },
    };
  },
  methods: {
    async getTagList() {
      const res = await this.$http.get(`${this.adminPre}/tags`);
      this.tagList = res.data;
    },
    // 弹窗关闭
    handleClose() {
      this.tag = {};
      this.dialogVisible = false;
    },
    showEdit(row) {
      this.tag = row;
      this.dialogVisible = true;
    },
    changeEdit() {
      this.$refs["tagrefs"].validate(async (valid) => {
        if (!valid) return;
        const res = await this.$http.post(`${this.adminPre}/tags`, this.tag);
        if (res.code == 200) {
          this.$message.success("成功！");
          this.getTagList();
          this.dialogVisible = false;
        } else {
          this.$message.error("失败");
        }
      });
    },
    async delTag(row) {
      this.$confirm("此操作将永久删除该标签, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          const res = await this.$http.delete(
            `${this.adminPre}/tags/${row.id}`
          );
          if (res.code == 200) {
            this.$message({
              type: "success",
              message: "删除成功!",
            });
            this.getTagList();
          } else {
            this.$message({
              type: "info",
              message: "删除失败!",
            });
          }
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    addshow() {
      this.tag = {};
      this.dialogVisible = true;
    },
  },
  created() {
    this.getTagList();
  },
};
</script>

<style scoped>
.card {
  background: #f1f2f3 !important;
}
</style>